package com.bridge.controler;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bridge.annotion.DictResponse;
import com.bridge.enums.UserSexEnum;
import com.bridge.exceyexcel.EasyExcelUtils;
import com.bridge.model.User;
import com.bridge.service.IUserService;
import com.bridge.utils.PageUtil;
import com.bridge.utils.ResUtils;
import com.bridge.vo.UserVO;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/getUsers")
    public List<User> getUsers() {
        List<User> users = userService.getAll();
        return users;
    }

    @DictResponse
    @RequestMapping("/listPage")
    public ResUtils<PageUtil<User>> listPage() {
        Page<User> userPage = userService.page(new Page<User>(), new QueryWrapper<User>());
        PageUtil<User> pageUtil = new PageUtil<>(userPage);
        return ResUtils.ok("", pageUtil);
    }

    @DictResponse
    @RequestMapping("/listPageVO")
    public ResUtils<PageUtil<UserVO>> listPageVO() {
        Page<User> page = userService.page(new Page<User>(1, 50), new QueryWrapper<User>());
        List<User> userList = page.getRecords();
        List<UserVO> userVOS = new ArrayList<>();
        userList.forEach(user -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            userVOS.add(userVO);
            userVO.setUser(user);
        });
        PageUtil userPage = new PageUtil(page, userVOS);
        return ResUtils.ok("", userPage);
    }


    @RequestMapping("/getUser")
    public User getUser(Long id) {
        User user = userService.getOne(id);
        return user;
    }

    @RequestMapping("/add")
    public void save(User user) {
        for (int i = 0; i < 1000; i++) {
            User user1 = new User();
            user1.setUserName(i % 2 == 0 ? "admin" : "user1");
            user1.setUserSex(i % 2 == 0 ? UserSexEnum.MAN : UserSexEnum.WOMAN);
            user1.setPassWord("123" + i);
            user1.setNickName(i % 2 == 0 ? "管理员" : "张三");
            userService.save(user1);
        }
    }


    @RequestMapping(value = "update")
    public void update(User user) {
        userService.update(user);
    }

    @RequestMapping(value = "/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        userService.delete(id);
    }


    @RequestMapping(value = "exportExcel")
    public void exportExcel(HttpServletResponse response) {
        try {
            EasyExcelUtils.easyExcelTest(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}